<template>
  <div>
    <headerh />
    <div class="login-container" ref="login_box">
      <div class="containers">
        <div class="section s_flex s_align_items s_justify_content">
          <div class="loginBg">
            <img src="../../../public/login.png" alt="" />
          </div>
          <div>
            <el-form
              v-if="loginIndex == 1"
              ref="loginForm"
              :model="loginForm"
              :rules="loginRules"
              class="login-form"
              autocomplete="on"
              label-position="left"
            >
              <div
                class="title-container s_justify_content s_flex s_align_items"
              >
                <h3
                  v-for="(item, index) in loginSize"
                  :class="loginIndexs == index ? 'Loginactive' : ''"
                  :key="index"
                  @click="getLogIndex(index)"
                >
                  {{ item }}
                </h3>
              </div>
              <div v-if="visitorStatus == 3">
                <div>
                  <div class="thumb_img">
                    <img :src="thumb" alt="" />
                  </div>
                  <div class="thumb_obx">
                    <p>{{mobiles}}</p>
                    <div class="s_flex s_justify_content_center">
                      <p>
                        <svg class="icon" aria-hidden="true">
                          <use xlink:href="#icon-iconfontanquan"></use>
                        </svg>
                      </p>
                      <p>账号安全，放心登录</p>
                    </div>
                  </div>
                      <el-button
                    :loading="loading"
                    class="pubBtn"
                    style="width: 100%; margin: 40px 0; height: 44px"
                    type="button"
                    @click.native.prevent="handleLogin"
                    >登录</el-button
                  >

                  <p class="context reg" @click="visitorStatus = 0">
                    切换账号 >
                  </p>
                </div>
              </div>
              <div v-else>
                <div v-if="visitorStatus == 0">
                  <el-form-item prop="username">
                    <span class="svg-container">
                      <svg-icon icon-class="user" />
                    </span>
                    <el-input
                      ref="mobile"
                      v-model="loginForm.mobile"
                      placeholder="用户名"
                      name="mobile"
                      type="text"
                      tabindex="1"
                      autocomplete="on"
                    />
                  </el-form-item>

                  <el-tooltip
                    v-model="capsTooltip"
                    content="Caps lock is On"
                    placement="right"
                    manual
                  >
                    <el-form-item prop="password">
                      <span class="svg-container">
                        <svg-icon icon-class="password" />
                      </span>
                      <el-input
                        :key="passwordType"
                        ref="password"
                        v-model="loginForm.password"
                        :type="passwordType"
                        placeholder="密码"
                        name="password"
                        tabindex="2"
                        autocomplete="on"
                        @keyup.native="checkCapslock"
                        @blur="capsTooltip = false"
                        @keyup.enter.native="handleLogin"
                      />
                      <span class="show-pwd" @click="showPwd">
                        <svg-icon
                          :icon-class="
                            passwordType === 'password' ? 'eye' : 'eye-open'
                          "
                        />
                      </span>
                    </el-form-item>
                  </el-tooltip>

                  <el-row type="flex" justify="space-between">
                    <el-col>
                      <el-checkbox
                        :checked="loginForm.checked"
                        v-model="loginForm.checked"
                        label="记住密码"
                        name="type"
                      ></el-checkbox>
                      <!--   <el-checkbox-group v-model="loginForm.type">
              </el-checkbox-group> -->
                    </el-col>
                    <el-col style="text-align: right">
                      <span class="context wangji" @click="loginIndex = 3"
                        >忘记密码?</span
                      >
                    </el-col>
                  </el-row>

                  <el-button
                    :loading="loading"
                    class="pubBtn"
                    style="width: 100%; margin: 40px 0; height: 44px"
                    type="button"
                    @click.native.prevent="handleLogin"
                    >登录</el-button
                  >

                  <p class="context reg" @click="loginIndex = 2">
                    暂无账号，立即注册 >
                  </p>
                </div>
                <div v-if="visitorStatus == 1">
                  <el-form-item prop="username">
                    <span class="svg-container">
                      <svg-icon icon-class="user" />
                    </span>
                    <el-input
                      v-model="visitor.mobile"
                      placeholder="请输入电话号码"
                      name="mobile"
                      type="text"
                      tabindex="1"
                      autocomplete="on"
                    />
                  </el-form-item>
                  <el-form-item class="ap_d">
                    <el-input
                      v-model="visitor.graphical"
                      placeholder="验证码"
                      type="text"
                      tabindex="1"
                      style="width: 100%"
                    >
                      <div
                        slot="append"
                        @click="refreshCode()"
                        style="cursor: pointer; background-color: none"
                        title="点击切换验证码"
                      >
                        <codeTemp :identifyCode.sync="identifyCode" />
                      </div>
                    </el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-input
                      v-model="visitor.code"
                      placeholder="验证码"
                      type="text"
                      tabindex="1"
                      style="width: 100%"
                    >
                      <el-button
                        slot="append"
                        :disabled="!!smsT2"
                        @click="getSmsVisitor"
                        >{{ smsT2 ? `${smsT2}s` : "获取验证码" }}</el-button
                      >
                    </el-input>
                  </el-form-item>

                  <el-button
                    :loading="loading"
                    class="pubBtn"
                    style="width: 100%; margin: 40px 0; height: 44px"
                    type="button"
                    @click.native.prevent="visitorLogins"
                    >登录</el-button
                  >

                  <p class="context reg" @click="loginIndex = 2">
                    暂无账号，立即注册 >
                  </p>
                </div>
              </div>
            </el-form>
            <el-form
              v-else-if="loginIndex == 2"
              class="login-form login-formh"
              autocomplete="on"
              label-position="left"
            >
              <div class="title-container">
                <h3 class="title2">申请注册</h3>
              </div>
              <el-form-item>
                <el-input
                  v-model="registerArr.company"
                  placeholder="请输入您的公司全称"
                  type="text"
                  tabindex="1"
                />
              </el-form-item>

              <el-form-item>
                <el-input
                  v-model="registerArr.username"
                  placeholder="请输入您的姓名"
                  type="text"
                  tabindex="1"
                />
              </el-form-item>

              <el-form-item>
                <el-input
                  v-model="registerArr.mobile"
                  placeholder="请输入您的手机号"
                  maxlength="11"
                  type="text"
                  tabindex="1"
                />
              </el-form-item>

              <el-form-item>
                <el-input
                  v-model="registerArr.email"
                  placeholder="请输入您的邮箱"
                  type="text"
                  tabindex="1"
                />
              </el-form-item>

              <el-button
                :loading="loading"
                class="pubBtn tj"
                type="button"
                @click.native.prevent="Addregister"
                >提交</el-button
              >

              <p class="context reg" @click="loginIndex = 1">立即登录 ></p>
            </el-form>

            <el-form
              v-else-if="loginIndex == 3"
              class="login-form"
              autocomplete="on"
              label-position="left"
            >
              <div class="title-container">
                <h3 class="title">找回密码</h3>
              </div>

              <el-form-item>
                <el-input
                  v-model="RetrieveArr.name"
                  placeholder="姓名"
                  type="text"
                  tabindex="1"
                />
              </el-form-item>

              <el-form-item>
                <el-input
                  maxlength="11"
                  v-model="RetrieveArr.mobile"
                  placeholder="手机号"
                  type="text"
                  tabindex="1"
                />
              </el-form-item>

              <el-form-item>
                <el-input
                  v-model="RetrieveArr.code"
                  placeholder="验证码"
                  type="text"
                  tabindex="1"
                  style="width: 100%"
                >
                  <el-button slot="append" :disabled="!!smsT" @click="getSms">{{
                    smsT ? `${smsT}s` : "获取验证码"
                  }}</el-button>
                </el-input>
              </el-form-item>

              <el-button
                :loading="loading"
                class="pubBtn"
                @click="Retrieve"
                type="button"
                style="width: 100%; margin: 40px 0; height: 44px"
                >找回密码</el-button
              >
              <p class="context reg" @click="loginIndex = 1">
                已想起密码,立即登录 >
              </p>
            </el-form>
            <el-form
              v-else-if="loginIndex == 4"
              class="login-form"
              autocomplete="on"
              label-position="left"
            >
              <div class="title-container">
                <h3 class="title2">提交成功</h3>
              </div>
              <p class="sz" style="color: #8f9bb3; line-height: 1.5">
                您的申请已提交。我们将在两个工作日内与您联系，请确保手机通讯畅通。请扫描关注下方二维码获得申请进度及通知。
              </p>
              <img class="ewm" src="../../../public/ewm.png" alt="" />
              <el-button
                class="pubBtn"
                type="button"
                style="width: 100%; margin: 10px auto 20px; height: 44px"
                @click="loginIndex = 1"
                >关闭</el-button
              >
              <!-- <p class="close" @click="loginIndex = 1">关闭</p> -->
            </el-form>
          </div>
        </div>
      </div>
      <!-- <p class="sz" style="color:#8F9BB3;text-align: center;"> 版权所有 © 2019-2020 顺企知识产权保护服务中心 ｜ 粤ICP备2023010395号</p> -->
    </div>
    <Footer />
  </div>
</template>

<script>
import headerh from "@/components/Header_h";
import Footer from "@/components/Footer";
import codeTemp from "@/components/codeTemp";
import { validUsername } from "@/utils/validate";
import { register, getSmsCode, forget, visitorLogin } from "@/api/user";
export default {
  components: {
    headerh,
    Footer,
    codeTemp,
  },
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请输入手机号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      smsT: 0,
      smsT2: 0,
      loginIndex: 1,
      visitorStatus:0,
      identifyCode: "",
      thumb_s:true,
      mobiles:localStorage.getItem("mobile"),
      identifyCodes: [
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "a",
        "b",
        "c",
        "d",
      ], //根据实际需求加入自己想要的字符
      loginSize: ["登录平台", "访客登录"],
      loginIndexs: 0,
      loginForm: {
        mobile: "",
        password: "",
        checked: false,
      },
      registerArr: {
        company: "",
        username: "",
        position: "",
        mobile: "",
        email: "",
      },
      RetrieveArr: {
        name: "",
        mobile: "",
        code: "",
      },
      loginRules: {
        mobile: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      visitor: {
        mobile: "",
        code: "",
        graphical: "",
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      thumb: localStorage.getItem("avatar"),
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  mounted() {
    if(this.thumb){
      this.visitorStatus = 3;
    }else{
      this.visitorStatus = 0;
    }
    this.refreshCode();
    const mobile = localStorage.getItem("mobile");
    const password = localStorage.getItem("password");
    this.$nextTick(() => {
      if (window.location.href.split("=")[1] == 1) {
        this.loginIndex = 1;
      } else if (window.location.href.split("=")[1] == 2) {
        this.loginIndex = 2;
      } else if (window.location.href.split("=")[1] == 3) {
        this.loginIndex = 3;
      }
    });

    if (mobile && password) {
      this.loginForm.checked = true;
      this.loginForm.mobile = mobile;
      this.loginForm.password = password;
    } else {
      this.loginForm.checked = false;
      this.loginForm.mobile = "";
      this.loginForm.password = "";
    }
  },
  methods: {
    getLogIndex(index) {
      this.loginIndexs = index;
      if(index == 0){
        this.visitorStatus = 3
      }else{
        this.visitorStatus = index;
      }
    },
    checkCapslock({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        if (
          (shiftKey && key >= "a" && key <= "z") ||
          (!shiftKey && key >= "A" && key <= "Z")
        ) {
          this.capsTooltip = true;
        } else {
          this.capsTooltip = false;
        }
      }
      if (key === "CapsLock" && this.capsTooltip === true) {
        this.capsTooltip = false;
      }
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery,
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    visitorLogins() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/visitor", this.visitor)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery,
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          // console.log("error submit!!");
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
    async getSms() {
      if (!this.RetrieveArr.mobile) {
        this.$message.error("请先输入手机号码");
        return;
      }else {
        try {
          await getSmsCode({
            mobile: this.RetrieveArr.mobile,
          });
          this.$message.success("验证码发送成功");
          this.smsT = 60;
          const smsid = setInterval(() => {
            this.smsT = this.smsT - 1;
            if (this.smsT == 0) {
              clearInterval(smsid);
            }
          }, 1000);
        } catch (e) {
          this.$message.error(e.message);
        }
      }
    },
    async getSmsVisitor() {
      if (!this.visitor.mobile) {
        this.$message.error("请先输入手机号码");
        return;
      } else if(this.visitor.graphical != this.identifyCode){
        this.$message.error("图形验证码错误");
        return;
      } else {
        try {
          await getSmsCode({
            mobile: this.visitor.mobile,
          });
          this.$message.success("验证码发送成功");
          this.smsT2 = 60;
          const smsid = setInterval(() => {
            this.smsT2 = this.smsT2 - 1;
            if (this.smsT2 == 0) {
              clearInterval(smsid);
            }
          }, 1000);
        } catch (e) {
          this.$message.error(e.message);
        }
      }
    },
    async Addregister() {
      try {
        const { company, username, position, mobile, email } = this.registerArr;
        await register({
          company,
          username,
          position,
          mobile,
          email,
        });
        this.$message.success("申请成功");
        this.loginIndex = 4;
      } catch (e) {
        this.$message.error(e.message);
      }
    },
    async Retrieve() {
      try {
        const { name, mobile, code } = this.RetrieveArr;
        await forget({
          name,
          mobile,
          code,
        });
        this.$message.success("重置成功，你的新密码已发送至邮箱");
        this.loginIndex = 1;
      } catch (e) {
        this.$message.error(e.message);
      }
    },
    // 生成随机数
    randomNum(min, max) {
      max = max + 1;
      return Math.floor(Math.random() * (max - min) + min);
    },
    // 更新验证码
    refreshCode() {
      this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
    },
    // 随机生成验证码字符串
    makeCode(data, len) {
      for (let i = 0; i < len; i++) {
        this.identifyCode += this.identifyCodes[
          this.randomNum(0, this.identifyCodes.length - 1)
        ];
      }
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #1f2e4c;
$cursor: #1f2e4c;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.loginBg {
  width: 50%;
  // position: absolute;
  // left:12%;
  // top:50%;
  // transform: translateY(-50%);
  img {
    width: 100%;
  }
}
.login-container {
  position: relative;
  padding:80px 0;
  .tj {
    width: 100%;
    margin: 40px 0;
    height: 44px;
  }
  .head {
    position: fixed;
    right: 380px;
    top: 50px;
    img {
      margin: 40px;
    }
  }
  // .section{
  //   display: flex;
  //   height:750px;
  // }
  .wangji {
    color: #3478ff;
    cursor: pointer;
  }
  .reg {
    color: #34bf92;
    text-align: center;
    cursor: pointer;
  }
  .el-form-item__content {
    background-color: #fff;
    width: 372px;
    height: 40px;
    border: 1px solid rgba(212, 219, 235, 1);
    border-radius: 4px;
    overflow: hidden;
  }
  .el-input {
    // display: inline-block;
    width: 90%;
    height: 40px;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 40px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px white inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #f7fbfe;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .close {
    text-align: center;
    font-size: 16px;
    font-weight: 400;
    color: #8f9bb3;
  }
  .ewm {
    width: 225px;
    height: 225px;
    display: block;
    margin: 16px auto 0;
  }
  .login-form {
    width: 457px;
    height: 480px;
    background: rgba(255, 255, 255, 1);
    box-shadow: 2px 11px 46px 0px rgba(0, 134, 254, 0.09);
    border-radius: 10px;
    max-width: 100%;
    // padding: 160px 35px 0;
    padding: 0 42px;
    overflow: hidden;
    background-color: #fff;
    // position:absolute;
    // right:310px;
    // top: 50%;
    // transform: translateY(-50%);
  }
  .login-formh {
    height: 530px;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    // padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    line-height: 30px;
    display: inline-block;
    text-align: center;
  }

  .title-container {
    width: 60%;
    position: relative;
    h3 {
      font-size: 20px;
      font-weight: 400;
      color: #666666;
      line-height: 14px;
      text-align: left;
      margin: 60px 0 40px 0;
      padding-bottom: 20px;
      cursor: pointer;
    }
    .Loginactive {
      font-size: 24px;
      font-weight: 400;
      color: #0f6ac7;
      line-height: 14px;
      // border-bottom:3px solid #0F6AC7;
      position: relative;
      &::before {
        content: "";
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 3px;
        background-color: #0f6ac7;
      }
    }
    // .title {
    //   font-weight: 400;
    //   font-size: 20px;
    //   color: #3478ff;
    //   text-align: left;
    //   margin: 60px 0 40px 0;
    // }

    .title2 {
      font-weight: 400;
      font-size: 20px;
      color: #3478ff;
      text-align: left;
      margin: 62px 0 30px 0;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 6px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }
  .sz {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: 5%;
  }
  // @media only screen and (max-width: 470px) {
  //   .thirdparty-button {
  //     display: none;
  //   }
  // }
}
.thumb_img {
  width: 79px;
  height: 79px;
  background: #fffefe;
  border: 1px solid #bccae4;
  border-radius: 39px;
  margin: 0 auto;
  img {
    width: 100%;
    padding: 10px;
  }
}
.icon {
  width: 20px;
  height: 15px;
}
.thumb_obx{
  text-align: center;
  p{
    margin-top:20px;
  }
}
// @media (max-width: 1680px) {
//   .login-container .head{
//     right:15%;
//   }
//   .head img{
//     margin:0 40px;
//   }
//   .loginBg img{
//     width:604px;
//     height:585px;
//   }
//   .login-container .section{
//     height:600px;
//   }
//   .login-container .login-form{
//     right: 10%;
//     transform: scale(.9) translateY(-50%);
//   }
// }
// @media (max-width:1440px) {
//   .head img{
//     margin:0 40px;
//   }
// }
// @media (max-width:750px) {
//   .el-input{
//     position: absolute;
//   }
//   .loginBg img{
//     width:604px;
//     height:585px;
//     display: none;
//   }
//   .el-form-item {
//       margin-bottom: 20px;
//   }
//   .login-container .head{
//       left: 50%;
//       transform: translateX(-50%);
//       right: 0;
//       top:5%;
//   }
//   .loginBg{
//     left:0;
//   }
//   .head img{
//       margin: 0 auto;
//       width: 100%;
//       height:100%;
//   }

//   .login-container .section{
//     height:600px;
//   }
//   .login-container .login-form{
//     width: 90%;
//     left:50%;
//     top:50%;
//     height:450px;
//     transform:translate(-50%,-50%);
//   }
//   .login-container .title-container .title,.title-container .title2{
//         margin:50px 0 35px 0 !important;
//   }
//   .login-container .section {
//       height: 490px;
//   }
//   .sz{
//     width:90%;
//     font-size:14px;
//     margin:0 auto;
//     text-align: center;
//   }
//   .tj{
//     margin:7px 0;
//   }
// }
</style>
